<?php
/**
 * Created by PhpStorm.
 * User: wjm
 * Date: 2017-04-24
 * Time: 上午 11:17
 * Versions：v1.00
 */

namespace app\hongyanche\controller;


class MallAdmin
{
    private $auth;
    public function __construct()
    {
        model('product') ->demand('mall');
        $auth = model('admin') -> is('mall');
        if($auth['state']){
            $this->auth = $auth['state'];
        }
    }
    //labels  获取标签
    public function labels(){
        $res = model('label')->rank('mall');
        return json($res);
    }
    //abels_modify 修改标签信息
    public function label_modify(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $data = json_decode(input('value'), 1);
        $res = model('label')->modify("mall", $data);
        return json($res);
    }


    //product_remove 产品删除
    public function product_remove(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $mall_id = input('mall_id');
        $res = model('product')->remove($mall_id);
        return json($res);
    }
    //product_range 产品排序
    public function product_range(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $range = input('range');
        $res = model('product')->product_range($range);
        return json($res);
    }
    //product_create  产品创建
    public function product_create(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $product_type_id = input('labels');
        $res = model('product')->add($product_type_id);
        return json($res);
    }
    //product_modify  产品编辑
    public function product_modify(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $mall_id = input('mall_id');
        $key = input('key');
        $modifyinfo = input('content');
        $res = model('product')->modify($mall_id, $key, $modifyinfo);
        return json($res);
    }
    //product_getlist 获取产品列表
    public function product_getlist(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $filer['page'] = input('page');
        $filer['maxPerPage'] = input('maxPerPage');
        $filer['keywords'] = input('keywords');
        $filer['select'] = input('labels');
        $res = model('mall')->getListForAdmin($filer);
        return json($res);
    }
    //product_release 开放/关闭
    public function product_release(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $mall_id = input('mall_id');
        $release = input('release');
        $res = model('product')->release($mall_id,$release);
        return json($res);
    }
    //product_highlight 设置高亮
    public function product_highlight(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $mall_id = input('mall_id');
        $cancel = input('cancel');
        $res = model('product')->highlight($mall_id,$cancel);
        return json($res);
    }


    //config_create  创建配置ceng
    public function config_create(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $data['labels'] = input('labels');
        $data['config_template_id'] = input('config_template_id');
        $data['mall_id'] = input('mall_id');
        $data['confing_descripton'] = input('confing_descripton');
        $res = model('mall')->config_create($data);
        return json($res);
    }
    //config_rename  修改配置层
    public function config_rename(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $config_id = input('config_id');
        $lang = input('lang');
        $config_name = input('newname');
        $describe = input('describe');
        $res = model('mall')->config_rename($config_id,$config_name,$lang,$describe);
        return json($res);
    }
    //config_range 排序
    public function config_range(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $range = input('range');
        $res = model('product')->config_range($range);
        return json($res);
    }
    //config_remove 删除配置
    public function config_remove(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $config_id = input('config_id');
        $res = model('mall')->config_remove($config_id);
        return json($res);
    }

    //配置描述
    public function config_descripton()
    {
        $config_descripton = input('config_descripton');
        $config_id = input('config_id');
        $res = model('mall')->config_descripton($config_id,$config_descripton);
        return json($res);
    }
    //config_release 开放/关闭
    public function config_release(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $config_ids = input('config_id');
        $release = input('release');
        $res = model('mall')->config_release($config_ids,$release);
        return json($res);
    }
    //config_getlist  获取配置层列表
    public function config_getList(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $config_template_id = input('config_template_id');
        $res = model('mall')->config_adminList($config_template_id);
        return json($res);
    }
    //config_option_clean 清空配置层
    public function config_option_clean(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $config_id = input('config_id');
        $res = model('product')->config_option_clean($config_id);
        return json($res);
    }
    //config_option_range 配置层排序
    public function config_option_range(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $config_id = input('config_id');
        $range = input('range');
        $res = model('product')->config_option_range($config_id,$range);
        return json($res);
    }
    //config_option_remove 删除配置层
    public function config_option_remove(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $config_option_ids = input('config_option_id');
        $res = model('product')->config_option_remove($config_option_ids);
        return json($res);
    }
    //config_option_create 配置层选项创建
    public function config_option_create(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $config_id = input('config_id');
        $config_option_name = input('config_option_name');
        $lang = input('lang');
        $res = model('product')->config_option_create($config_id,$config_option_name,$lang);
        return json($res);
    }
    //配置模板
    public function config_template_create()
    {
//        $data['name'] = input('template_name');
//        $data['config_ids'] = input('config_ids');
        $res = model('mall')->config_template_create();
        return json($res);
    }
    public function config_template_list()
    {
        $res = model('mall')->config_template_list($config_template_id=1);
        return json($res);
    }
    public function config_template_remove()
    {
        $config_template_id = input('config_template_id');
        $res = model('mall')->config_template_remove($config_template_id);
        return json($res);
    }
    public function config_template_modify()
    {
        $config_template_id = input('config_template_id');
        $new_template_name = input('new_template_name');
        $res = model('mall')->config_template_modify($config_template_id,$new_template_name);
        return json($res);
    }
    public function config_template_release()
    {
        $config_template_id = input('config_template_id');
        $release = input('release');
        $res = model('mall')->config_template_release($config_template_id,$release);
        return json($res);
    }
    // 排序
    public function config_template_range(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $range = input('range');
        $res = model('mall')->config_template_range($range);
        return json($res);
    }
    //detail_range 型号排序
    public function detail_range(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $range = input('range');
        $res = model('product')->detail_range($range);
        return json($res);
    }
    public function detail_create(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $data['price'] = input('price');
        $data['deposit'] = input('deposit');
        $data['detail_code'] = input('code');
        $data['options'] = input('options');
        $data['discount'] = input('discount');
        $data['discount_price'] = input('discount_price');
//        $data['deposit'] = input('deposit');
        $data['mall_id'] = input('mall_id');
        $res = model('product')->detail_create($data);
        return json($res);
    }
    //detail_remove 删除型号
    public function detail_remove(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $detail_ids = input('detail_id');
        $res = model('product')->detail_remove($detail_ids);
        return json($res);
    }
    //detail_release 开放/关闭
    public function detail_release(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $config_ids = input('detail_id');
        $release = input('release');
        $res = model('product')->detail_release($config_ids,$release);
        return json($res);
    }
    //detail_getlist  获取型号列表
    public function detail_getlist(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $filer['page'] = input('page');
        $filer['maxPerPage'] = input('maxPerPage');
        $filer['select'] = input('mall_id');
        $res = model('product')->detail_adminList($filer);
        return json($res);
    }


    //menu  获取菜单
    public function menu(){
        $filer['keywords'] = input('keywords');
        $filer['select'] = input('labels');
        $res = model('product')->getList($filer);
        return json($res);
    }
    //get_info 获取信息
    public function get_info(){
        //检查权限
        if (!$this->auth){
            return json(['state' => 0,'errormsg' => 'authority incorrect']);
        }
        $mall_id = input('mall_id');
        $res = model('product')->getInfo($mall_id);
        return json($res);
    }


    //statistics_today 今日统计
    public function briefing(){
        $res = model('mall')->statisticsToday();
        return json($res);
    }
    //order_new 查看新订单
    public function order_new(){
        $time = input('time');
        $res = model('mall')->orderNew($time);
        return json($res);
    }
    //order_ready 准备完成
    public function order_ready(){
        $cafe_order_id = input('mall_order_id');
        $order_id = input('mall_id');
        $res = model('mall')->orderReday($cafe_order_id,$order_id);
        return json($res);
    }
    //order_create  创建订单
    public function order_create(){
        $detail = input('detail');
        $card_code = input('card_code');
        $res = model('mall')->adminOrderCreate($detail,$card_code);
        return json($res);
    }
    //mall_order_modify 修改mall订单状态
    public function mall_order_modify(){
        $mall_order_id = input('mall_order_id');
        $res = model('mall')->mallOrderModify($mall_order_id);
        return json($res);
    }
    //get_mallorder_list 获取mall订单列表
    public function get_order_list(){
        $filer['page'] = input('page');
        $filer['maxPerPage'] = input('maxPerPage');
        $res = model('mall')->getmallOrderList($filer);
        return json($res);
    }


    //refund_record   产品退款清单
    public function refund_record(){
        $filter['page'] = input('page');
        $filter['maxPerPage'] = input('maxPerPage');
        $filter['keywords'] = input('keywords');
        $channel = input('channel');
        $res = model('order')->getRefundList('mall',$filter,$channel);
        return json($res);
    }
    //首页推荐suggest = 1,
    public function product_suggest(){
        $mall_id = input('mall_id');
        $suggest = input('suggest');
        $res  = model('mall')->product_suggest($mall_id,$suggest);
        return json($res);
    }
    //order_getlist_today 今日订单记录
    public function order_getlist_today(){
        $filer['page'] = input('page');
        $filer['maxPerPage'] = input('maxPerPage');
        $filer['keywords'] = input('keywords');
        $filer['select'] = input('select');
        $unpaid = input('unpaid');
        $channel = input('channel');
        $res = model('mall')->getOrderList($filer,$demand='',$unpaid,$channel) ;
        return json($res);
    }

    //判断是否有权限更改模板和标签
    public function isAdmin()
    {
        $config = getConfig('admin_chief');
        if(in_array(model('user')->getUserId(),$config)){
            return true;
        }else{
            return false;
        }
    }


    //库存
}